/*
 * @Date: 2024-02-28 14:46:23
 * @LastEditors: lyx
 * @LastEditTime: 2024-03-06 19:41:12
 * @FilePath: \taro-template3\src\components\MsgContent\hooks\useMsgContent.tsx
 * @Description: 
 */
import { ACTON_OPTIONS, MSG_ACTION_PUP_TOAST_ID } from "@/components/MsgActionPup/hooks/store";
import { ChatContext } from "@/pages/index/hooks/store";
import { Toast } from "@nutui/nutui-react-taro";
import Taro from "@tarojs/taro";
import React, { useContext, useState } from "react";

function useMsgContent() {
  const { reLoad, toPlay, toFeedback } = useContext(ChatContext);

  const [customized, setCustomized] = useState(false);
  const [pupData, setPupData] = useState<any>({});
  const handleLongPress = (e: Event, item: any) => {
    console.log("handleLongPress e", e);
    setPupData({
      event: e,
      item,
    });
  };

  const handleClick = (item: IMsgActionItem) => {
    const { item: itemData = {} } = pupData;
    const content = itemData?.messageBody?.contents?.[0]?.content ?? "";
    if (item.key === "copy") {
      Taro.setClipboardData({
        data: content,
      });
    } else if (item.key === ACTON_OPTIONS.reload) {
      // TODO
      reLoad(itemData);
    } else if (item.key === ACTON_OPTIONS.play) {
      // TODO
      toPlay(itemData);
    } else if (item.key === ACTON_OPTIONS.thumbsUp) {
      if (itemData?.feedback === ACTON_OPTIONS.thumbsUp) {
        return;
      }
      toFeedback({
        feedback: ACTON_OPTIONS.thumbsUp,
        id: itemData.id
      });
    } else if (item.key === ACTON_OPTIONS.thumbsDown) {
      if (itemData?.feedback === ACTON_OPTIONS.thumbsDown) {
        return;
      }
      toFeedback({
        feedback: ACTON_OPTIONS.thumbsDown,
        id: itemData.id
      });
    } else {
      Toast.show(MSG_ACTION_PUP_TOAST_ID, {
        title: "操作成功",
        type: "text",
        duration: 1,
        lockScroll: true,
        onClose: () => {
          console.log("close");
        },
      });
    }
  }

  return {
    customized,
    pupData,
    handleLongPress,
    setCustomized,
    handleClick,
  };
}

export default useMsgContent;
